package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName : SpecController  //类名
 * @Description : 规格controller  //描述
 * @Author : qingfeng  //作者
 * @Date: 2020-09-17 20:33  //时间
 */
@RestController
@RequestMapping("spec")
public class SpecController {
    private SpecGroupService groupService;
    private SpecParamService paramService;
    public SpecController(SpecGroupService groupService, SpecParamService paramService){
        this.groupService = groupService;
        this.paramService = paramService;
    }

    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategory(@RequestParam("id")Long id){
        /**
         * @Description //TODO 根据分类查询规格组
         * @Param [id]
         * @return org.springframework.http.ResponseEntity<java.util.List < com.leyou.item.dto.SpecGroupDTO>>
         */
        return ResponseEntity.ok(SpecGroupDTO.convertEntityList(
                groupService.query().eq("category_id",id).list()
        ));
    }

    @GetMapping("params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(@RequestParam(value = "categoryId",required = false)Long categoryId,
                                                              @RequestParam(value = "groupId",required = false)Long groupId,
                                                              @RequestParam(value = "searching",required = false)Boolean searching){
        /**
         * @Description //TODO 查询规格参数集合
         * @Param [categoryId, groupId, searching]
         * @return org.springframework.http.ResponseEntity<java.util.List < com.leyou.item.dto.SpecParamDTO>>
         */
        return ResponseEntity.ok(paramService.querySpecParams(categoryId,groupId,searching));
    }

    @PostMapping("group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO specGroupDTO){
        /**
         * @Description //TODO 新增规格组
         * @Param [specGroupDTO]
         * @return org.springframework.http.ResponseEntity<java.lang.Void>
         */
        //新增规格组
        groupService.save(specGroupDTO.toEntity(SpecGroup.class));
        //返回结果
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO groupDTO){
        /**
         * @Description //TODO 修改规格组
         * @Param [groupDTO]
         * @return org.springframework.http.ResponseEntity<java.lang.Void>
         */
        //更新规格组
        groupService.updateById(groupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PostMapping("param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO paramDTO){
        /**
         * @Description //TODO 新增规格参数
         * @Param [paramDTO]
         * @return org.springframework.http.ResponseEntity<java.lang.Void>
         */
        paramService.save(paramDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO paramDTO){
        /**
         * @Description //TODO 修改规格参数
         * @Param [paramDTO]
         * @return org.springframework.http.ResponseEntity<java.lang.Void>
         */
        paramService.updateById(paramDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id")Long categoryId){
        /**
         * @Description //TODO 查询规格组及规格参数
         * @Param [categoryId]
         * @return org.springframework.http.ResponseEntity<java.util.List < com.leyou.item.dto.SpecGroupDTO>>
         */
        return ResponseEntity.ok(groupService.querySpecList(categoryId));
    }

}
